package com.yanqu.road.server.manager.activity.feast.task;

import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.server.manager.activity.feast.CrossFeastMgr;
import com.yanqu.road.server.manager.activity.feast.activity.CrossFeastActivity;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.executor.GeneralTask;

import java.util.Map;

/**
 * pvp定时任务
 */
public class FeastPvpTask extends GeneralTask {

    private long debugTestTime;

    private long checkActivityTime;

    private long checkSettleTime;

    @Override
    public void doRun() {
        Map<Integer, CrossFeastActivity> activityMap = CrossFeastMgr.getActivityMap();
        if (activityMap == null || activityMap.isEmpty()) {
            // 无活动
            return;
        }

        // 活动时间 次/1s
        if (System.currentTimeMillis() > checkActivityTime) {
            checkActivityTime = System.currentTimeMillis() + DateHelper.SECOND_MILLIONS;
            for (CrossFeastActivity feastActivity : activityMap.values()) {
                try {
                    if (!ActivityHelper.activityInTime(feastActivity.getConfig().getActivityInfo())) {
                        continue;
                    }
                    feastActivity.crossFeastPvpActivityCheckTask();
                } catch (Exception e) {
                    getLOGGER().error("checkActivityTimeTask error : ", e);
                }
            }
        }

        // 检测结算 次/1s
        if (System.currentTimeMillis() > checkSettleTime) {
            checkSettleTime = System.currentTimeMillis() + DateHelper.SECOND_MILLIONS;
            for (CrossFeastActivity daoMuActivity : activityMap.values()) {
                try {
                    if (!ActivityHelper.activityInTime(daoMuActivity.getConfig().getActivityInfo())) {
                        continue;
                    }
                    daoMuActivity.checkSettleTask();
                } catch (Exception e) {
                    getLOGGER().error("checkSettleTask error : ", e);
                }
            }
        }

        // 调试定时器 次/10s
        if (System.currentTimeMillis() > debugTestTime) {
            debugTestTime = System.currentTimeMillis() + DateHelper.SECOND_MILLIONS * 10;
            for (CrossFeastActivity feastActivity : activityMap.values()) {
                try {
                    if (!ActivityHelper.activityInTime(feastActivity.getConfig().getActivityInfo())) {
                        continue;
                    }
                    feastActivity.crossFeastEmptyTask();
                } catch (Exception e) {
                    getLOGGER().error("feastActivityPvpEmptyTask error : ", e);
                }
            }
        }

    }
}
